package com.cultraview;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JLabel;

public class MouseLable extends JLabel {

	private static final int SIDELENGTH = 10;
	private ArrayList<Rectangle2D> squares;
	private Rectangle2D current; // the square containing the mouse cursor
	private MouseOperateCallBack mCallBack;
	private Point startPoint,endPoint;
	
	public MouseLable(Icon icon) {
		init();
		setIcon(icon);
	}

	private void init() {
		squares = new ArrayList<>();
		current = null;
		addMouseListener(new MouseHandler());
		addMouseMotionListener(new MouseMotionHandler());
	}
	
	public void setCallBack(MouseOperateCallBack callback){
		mCallBack = callback;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		if(current != null) {
			g2.draw(current);
		}
		for (Rectangle2D r : squares)
			g2.draw(r);
	}

	public Rectangle2D find(Point2D p) {
		for (Rectangle2D r : squares) {
			if (r.contains(p))
				return r;
		}
		return null;
	}

	public void add(Point2D p) {
		double x = p.getX();
		double y = p.getY();
		current = new Rectangle2D.Double(x - SIDELENGTH / 2,
				y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
		squares.add(current);
		repaint();
	}

	public void remove(Rectangle2D s) {
		if (s == null)
			return;
		if (s == current)
			current = null;
		squares.remove(s);
		repaint();
	}

	private class MouseHandler extends MouseAdapter {
		public void mousePressed(MouseEvent event) {
			//System.out.println("mousePressed ");
			if(event.getButton() == MouseEvent.BUTTON1) {
				startPoint = event.getPoint();
			}
		}

		public void mouseReleased(MouseEvent event) {
			//System.out.println("mouseReleased ");
			if(event.getButton() == MouseEvent.BUTTON1) {
				double x = Math.min(startPoint.x, event.getX());
				double y = Math.min(startPoint.y, event.getY());
				double width = Math.abs(startPoint.x - event.getX());
				double hight = Math.abs(startPoint.y - event.getY());
				current = null;
				if(width == 0 || hight == 0) return;
				mCallBack.addRect((int)x, (int)y, (int)width, (int)hight);
			}
		}
		
		public void mouseClicked(MouseEvent event) {
			//System.out.println("mouseClicked " + event.getButton());
			if(event.getButton() == MouseEvent.BUTTON3) {
				mCallBack.removeRect(event.getX(),event.getY());
			}
			if(event.getButton() == MouseEvent.BUTTON1) {
				mCallBack.addRect(event.getX(), event.getY(), 0, 0);
			}
		}
	}

	private class MouseMotionHandler implements MouseMotionListener {
		public void mouseMoved(MouseEvent event) {
			//System.out.println("mouseMoved x:" + event.getX() + " y:" + event.getY());
		}

		public void mouseDragged(MouseEvent event) {
			//System.out.println("mouseDragged x:" + event.getX() + " y:" + event.getY());
			if(startPoint != null) {
				double x = Math.min(startPoint.x, event.getX());
				double y = Math.min(startPoint.y, event.getY());
				double width = Math.abs(startPoint.x - event.getX());
				double hight = Math.abs(startPoint.y - event.getY());
				current = new Rectangle2D.Double(x, y, width, hight);
				//System.out.println(current.toString());
				repaint();
			}
		}
	}
	
	public interface MouseOperateCallBack {
		public void removeRect(int x, int y);
		
		public void addRect(int x, int y, int width, int hight);
	}
}
